package com.flyee.viewer.pack.helper
{
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;

	public class CellGridBox extends UIComponent
	{
		private const WIDTH_CELL:int=40;
		private const HEIGHT_CELL:int=40;
		/**
		 * 是否填充了数据
		 */
		private var isFill:Boolean=false;
		/**
		 * 编号
		 */
		private var cellId:String=null;
		/**
		 * 判断属于那个面板
		 */
		private var cellType:String=null;
		/**
		 * 背景
		 */
		private var bgPanel:Sprite=null;
		/**
		 * 道具
		 */
		private var prop:ItemViewModel=null;
		/**
		 * 在父级面板中的一个编号
		 */
		private var cellPos:int=0;
		/**
		 * 相对于父级面板的坐标
		 */
		private var cellPosition:Point=null;
		/**
		 * 道具提示信息
		 */
		private var message:String=null;
		/**
		 * 道具名称
		 */
		private var propName:String=null;
		/**
		 * 道具背景的形状集合
		 */
		private var graphicsArray:Array=null;
		/**
		 * 显示道具数量的标签
		 */
		private var countLab:TextField=null;
		private var countSprite:Sprite=null;
		private var count:int=0;

		public function CellGridBox()
		{
			bgPanel=new Sprite();
			this.cellPosition=new Point();
			createCell(bgPanel);
			bgPanel.x=WIDTH_CELL;
			countLab=createLab();
			countSprite=new Sprite();
			countSprite.addChild(countLab);
			this.mouseChildren=true;
			this.addChild(bgPanel);
		}

		/**
		 * 创建一个标签，该标签不具有任何交互事件
		 * @return
		 *
		 */
		private function createLab():TextField
		{
			var txt:TextField=new TextField();
			var format:TextFormat=new TextFormat("Birch Std", 15, 0xffffff, null);
			txt.autoSize=TextFieldAutoSize.CENTER;
			txt.selectable=false;
			txt.mouseEnabled=true;
			txt.mouseWheelEnabled=false;
			/* txt.background = true;
			 txt.backgroundColor = 0x000000; */
			txt.thickness=14;
			//txt.border = true;
			txt.defaultTextFormat=format;
			//ColorMagicOfText.bewrite(txt);
			return txt;
		}

		private function createCell(target:Sprite):void
		{
			graphicsArray=new Array();

			var shape1:Shape=new Shape();
			var shape2:Shape=new Shape();
			var shape3:Shape=new Shape();
			var colors:Array=[0xA5EDFC, 0xFFFFFF];
			var alphas:Array=[0, 0.9];
			var ratios:Array=[130, 225];
			var filter:GlowFilter=new GlowFilter();
			var filters:Array=new Array();

			filter.blurX=1.5;
			filter.blurY=1.5;
			filter.alpha=0.5;
			filter.quality=BitmapFilterQuality.HIGH;
			filter.strength=1;
			filter.knockout=false;
			filter.inner=false;
			filter.color=0X4195AB;
			filters.push(filter);

			shape1.graphics.beginFill(0x0f4e6c, 0.5);
			shape1.graphics.drawRect(2, 2, 36, 36);
			shape1.graphics.endFill();

			shape1.graphics.lineStyle(2, 0Xffffff, 0.2);
			shape1.graphics.drawRoundRect(0, 0, 40, 40, 8, 8);

			shape3.graphics.lineStyle(2, 0xFFFFFF, 1);
			shape3.graphics.lineGradientStyle(GradientType.LINEAR, colors, alphas, ratios);
			shape3.graphics.drawRoundRect(1, 1, 38, 38, 8, 8);
			shape3.blendMode=BlendMode.ADD;

			shape2.graphics.lineStyle(2, 0x0c3752, 1);
			shape2.graphics.drawRoundRect(1, 1, 38, 38, 8, 8);
			shape2.filters=filters;

			target.addChild(shape2);
			target.addChild(shape1);
			target.addChild(shape3);

			graphicsArray.push(shape1);
			graphicsArray.push(shape2);
			graphicsArray.push(shape3);

			target.rotation=90;
		}

		private function addListeners():void
		{
//			this.addEventListener(MouseEvent.MOUSE_DOWN,startDragHandler);
//			this.addEventListener(MouseEvent.MOUSE_OVER,clickHandler);
//			this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}

		private function removeListeners():void
		{
			this.removeEventListener(MouseEvent.CLICK, clickHandler);
			this.removeEventListener(MouseEvent.MOUSE_OVER, clickHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		private function clickHandler(e:MouseEvent):void
		{
		}

		private function overHandler(e:MouseEvent):void
		{
			if (this.isFill)
			{

			}
		}

		private function outHandler(e:MouseEvent):void
		{
		}

		private function startDragHandler(e:MouseEvent):void
		{
			if (this.contains(prop))
			{
				this.removeChild(prop);
			}
			prop.location=this.position;
			this.parent.addChild(prop);
		//this.removeData();
		}
		
		private function getGlowFilter():GlowFilter{
			var color:int = 0xff00ff;
			var alpha:Number = 0.8;
			return new GlowFilter(color,alpha,3,3);
		}

		public function setCellIndex(i:int):void
		{
			if (i > -1)
			{
				this.cellPos=i;
			}
		}

		public function isFilled():Boolean
		{
			return this.isFill;
		}

		public function set hitEffect(b:Boolean):void{
			if(b){
				this.filters = [getGlowFilter()];
			}else{
				this.filters.splice(0);
				this.filters = null;
			}
		}

		public function setPosition(cellxy:Point):void
		{
			if (cellxy.x > -1 && cellxy.y > -1)
			{
				this.cellPosition.x=cellxy.x + 1;
				this.cellPosition.y=cellxy.y + 1;
				this.x=cellxy.x;
				this.y=cellxy.y;
			}
			else
			{
				throw new Error("the index of cell is error!!! ");
			}
		}

		public function set stackCount(c:int):void
		{
			count=c;
			if (count > 1)
			{
				countLab.text=c.toString();
				if (!this.contains(countSprite))
				{
					countLab.x=WIDTH_CELL - countLab.width;
					countLab.y=HEIGHT_CELL - countLab.height;
					this.addChild(countSprite);
				}
			}
			else
			{
				countLab.text="";
			}
		}

		public function removeData():void
		{
			if (prop && this.contains(prop))
			{
				this.removeChild(prop);
			}
			this.cellId=null;
			this.cellType=null;
			this.isFill=false;
			this.prop=null;
			if (this.contains(countSprite))
			{
				this.removeChild(countSprite);
			}
			countLab.text="";

			removeListeners();
		}

		public function get cellID():String
		{
			return cellId;
		}

		public function get cellTypes():String
		{
			return cellType;
		}

		public function get position():Point
		{
			return this.cellPosition;
		}

		public function get cellIndex():int
		{
			return this.cellPos;
		}

		public function get absProp():ItemViewModel
		{
			return this.prop
		}

		public function get stackCount():int
		{
			return count;
		}

		public function addProp(item:ItemViewModel):void
		{
			this.cellId=item.dataModel.id;
			//this.cellType = prop.dataModel.panelTypes;
			this.prop=item;
			this.propName=item.dataModel.name;
			this.message=item.dataModel.tip;
			item.x=1;
			item.y=1;
			this.addChild(item);
			this.isFill=true;
			this.count=prop.dataModel.currentCapability;
			if (count > 1)
			{
				countLab.text=count.toString();
				countLab.x=WIDTH_CELL - countLab.width;
				countLab.y=HEIGHT_CELL - countLab.height;
				this.addChild(countSprite);
			}

		// addListeners();
		}

		public function clear():void
		{
			while(countSprite.numChildren > 0)
			{
				countSprite.removeChildAt(0);
			}
			if (prop)
			{
				if (this.contains(prop))
				{
					this.removeChild(prop);
				}
				prop.dispose();
				prop=null;
			}
			removeData();
			var tempLength:int=graphicsArray.length;
			for(var i:int=0; i < tempLength; i++)
			{
				var tempShape:Shape=graphicsArray[i]as Shape;
				if (bgPanel.contains(tempShape))
				{
					bgPanel.removeChild(tempShape);
				}
				tempShape.graphics.clear();
				tempShape=null;
			}
			this.graphicsArray.splice(0);
			if (this.owns(bgPanel))
			{
				this.removeChild(bgPanel);
			}

			countLab=null;
			countSprite=null;
			message=null;
			cellPosition=null;
			bgPanel=null;
		}
	}
}

